using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using DM.DAO;
using DM.Entity;

namespace DM.Web.Admin.QuanLyTinTuc
{
    public partial class DanhSachTinTuc : System.Web.UI.Page
    {
        readonly LoaiTinDAO loaiTinService = new LoaiTinDAO();
        readonly TinTucDAO tinTucService = new TinTucDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindingDropDownListTinCha();
                BindingDropDownListNgonNgu();
                BindingDropDownListLoaiTin();
            }
            BindingDataGrid();
        }
        /// <summary>
        /// Gắn kết DropDownListLoaiTinCha với CSDL
        /// </summary>
        private void BindingDropDownListTinCha()
        {
            List<LoaiTin> listMaTinCha = loaiTinService.getLoaiTinCha();
            DropDownListLoaiTinCha.Items.Add(new ListItem("Chọn tất cả","0"));
            foreach (LoaiTin loaiTin in listMaTinCha)
            {
                if (loaiTin.Ma != 17 && loaiTin.Ma!= 6 && loaiTin.Ma!=23)
                DropDownListLoaiTinCha.Items.Add(new ListItem(loaiTin.Tenloaitin,loaiTin.Ma.ToString()));
            }
            
        }
        /// <summary>
        /// Gắn kết DropDownListLoaiTin với CSDL sau khi chọn Loại Tin Cha
        /// </summary>
        private void BindingDropDownListLoaiTin()
        {
            DropDownListLoaiTin.Items.Clear();
            DropDownListLoaiTin.Items.Add(new ListItem("Chọn tất cả", "0"));
            if (DropDownListLoaiTinCha.SelectedValue=="0")
            {
                DropDownListLoaiTin.Enabled = false;
            }
            else
            {
                List<LoaiTin> listLoaiTin =
                loaiTinService.GetLoaiTin(Common.ParseInt32(DropDownListLoaiTinCha.SelectedValue));
                DropDownListLoaiTin.Enabled = true;
                foreach (LoaiTin loaiTin in listLoaiTin)
                {
                    DropDownListLoaiTin.Items.Add(new ListItem(loaiTin.Tenloaitin,loaiTin.Ma.ToString()));
                }
            }
        }
        /// <summary>
        /// Gắn kết DropDownListNgonNgu 
        /// </summary>
        private void BindingDropDownListNgonNgu()
        {
            DropDownListNgonNgu.Items.Add(new ListItem("Tiếng Việt","1"));
            DropDownListNgonNgu.Items.Add(new ListItem("Tiếng Anh","2"));
        }
        /// <summary>
        /// Gắn kết dữ liệu với GridView
        /// </summary>
        private void BindingDataGrid()
        {
            //Binding data to label


            //Bind data to grid view
            DataSet dataSet = new DataSet();
            if (DropDownListLoaiTinCha.SelectedValue =="0")
            {
                dataSet = tinTucService.GetTinTucAll(Common.ParseInt32(DropDownListNgonNgu.SelectedValue));
                LabelBangTin.Text = "Danh sách tất cả các loại tin";
            }
            else
            {
                if (DropDownListLoaiTin.SelectedValue == "0")
                {
                    dataSet =
                        tinTucService.GetTinTucByLoaiTinGocVaNgonNgu(
                            Common.ParseInt32(DropDownListLoaiTinCha.SelectedValue),
                            Common.ParseInt32(DropDownListNgonNgu.SelectedValue));
                    LabelBangTin.Text = "Danh sách các tin " + DropDownListLoaiTinCha.SelectedItem;
                }
                else
                {
                    dataSet =
                        tinTucService.GetTinTucByMaLoaiTinVaNgonNgu(
                            Common.ParseInt32(DropDownListLoaiTin.SelectedValue),
                            Common.ParseInt32(DropDownListNgonNgu.SelectedValue));
                    LabelBangTin.Text = "Danh sách các tin " + DropDownListLoaiTin.SelectedItem;
                }
            }
            DataView dataView = dataSet.Tables[0].DefaultView;
            dataView.Sort = ShortColumn + " " + (ShortAscend?"ASC":"DESC");
            GridViewTinTuc.DataSource = dataView;
            GridViewTinTuc.DataBind();
        }

        protected void DropDownListLoaiTinCha_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingDropDownListLoaiTin();
        }

        protected void GridViewTinTuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewTinTuc.PageIndex = e.NewPageIndex;
            BindingDataGrid();
        }
        /// <summary>
        /// Property Column nào shorting trong GridViewTinTuc
        /// </summary>
        private string ShortColumn
        {
            get
            {
                object o = ViewState["ShortColumn"];
                if (o == null) return "NgayDang";
                return (string) o;
            }
            set { ViewState["ShortColumn"] = value; }
        }
        /// <summary>
        /// Property Short theo hướng nào (Ascend hay Descend)
        /// </summary>
        private bool ShortAscend
        {
            get
            {
                object o = ViewState["ShortAscend"];
                if (o == null) return false;
                return (bool) o;
            }
            set { ViewState["ShortAscend"] = value; }
        }

        protected void GridViewTinTuc_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (e.SortExpression == ShortColumn) ShortAscend = !ShortAscend;
            else
            {
                ShortColumn = e.SortExpression;
                ShortAscend = true;
            }
            BindingDataGrid();
        }

        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            Button buttonEdit = (Button) sender;
            GridViewRow rowEdit = (GridViewRow) buttonEdit.Parent.Parent;
            string maTin = GridViewTinTuc.DataKeys[rowEdit.RowIndex].Value.ToString();
            Response.Redirect("ThemMoiTinTuc.aspx?MaTin="+maTin);
        }

        protected void ButtonXoa_Click(object sender, EventArgs e)
        {
            Button buttonXoa = (Button) sender;
            GridViewRow currentRow = (GridViewRow) buttonXoa.Parent.Parent;
            GridViewTinTuc.EditIndex = currentRow.RowIndex;
            GridViewTinTuc.DataBind();
        }

        protected void ButtonNoDel_Click(object sender, EventArgs e)
        {
            GridViewTinTuc.EditIndex = -1;
            GridViewTinTuc.DataBind();
        }

        protected void ButtonDel_Click(object sender, EventArgs e)
        {
            Button ButtonOk = (Button) sender;
            GridViewRow deleteRow = (GridViewRow) ButtonOk.Parent.Parent;
            string maTin = GridViewTinTuc.DataKeys[deleteRow.RowIndex].Value.ToString();
            int i = GridViewTinTuc.PageIndex;
            GridViewTinTuc.DeleteRow(deleteRow.RowIndex);
            tinTucService.Delete(maTin);
            Common.Show("Tin đã bị xóa!");
            Response.Redirect("DanhSachTinTuc.aspx");
        }

        protected void GridViewTinTuc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewTinTuc.EditIndex = -1;
            GridViewTinTuc.DataBind();
        }

        protected void btnThemMoiClick(object sender, EventArgs e)
        {
            Response.Redirect("ThemMoiTinTuc.aspx");
        }
    }
}
